<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    <script src="../js/pages/public.js"></script>

</head>

<body class="hold-transition skin-red sidebar-mini">
<!-- .box-body -->

<div class="box-header with-border">
    <h3 class="box-title">商品分类管理
    </h3>
</div>

<div class="box-body">
    <ol class="breadcrumb" id="item">

    </ol>

    <!-- 数据表格 -->
    <div class="table-box">

        <!--工具栏-->
        <div class="pull-left">
            <div class="form-group form-inline">
                <div class="btn-group">
                    <button type="button" class="btn btn-default" title="新建" data-toggle="modal"
                            data-target="#editModalAdd"><i class="fa fa-file-o"></i> 新建
                    </button>
                    <button type="button" class="btn btn-default" title="删除" onclick="deleteItemCat()"><i
                            class="fa fa-trash-o"></i> 删除
                    </button>
                    <button type="button" class="btn btn-default" title="刷新"><i class="fa fa-check"></i> 刷新</button>

                </div>
            </div>
        </div>


        <!--数据列表-->
        <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
            <thead>
            <tr>
                <th class="" style="padding-right:0px">
                    <input type="checkbox" class="icheckbox_square-blue">
                </th>
                <th class="sorting_asc">分类ID</th>
                <th class="sorting">分类名称</th>
                <th class="sorting">类型模板ID</th>
                <th class="text-center">操作</th>
            </tr>
            </thead>
            <tbody id="cat" name="cat">

            </tbody>
        </table>
        <!--数据列表/-->

    </div>
    <!-- 数据表格 /-->


</div>
<!-- /.box-body -->


<!-- 新建窗口 -->
<div class="modal fade" id="editModalAdd" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="ModalLabel">商品分类编辑</h3>
            </div>
            <div class="modal-body">
                <input id="aaa" type="hidden">
                <table class="table table-bordered table-striped" width="800px">
                    <tr>
                        <td>上级商品分类</td>
                        <td id="addName">
                            <input type="hidden" value="0"><a>顶级分类列表</a>
                        </td>
                    </tr>
                    <tr>
                        <td>商品分类名称</td>
                        <td><input class="form-control" placeholder="商品分类名称" id="itName"></td>
                    </tr>
                    <tr>
                        <td>类型模板</td>
                        <td>
                            <input config="options['type_template']" placeholder="商品类型模板" class="form-control"
                                   type="text" value="35" id="itypeId"/>
                        </td>
                    </tr>
                </table>

            </div>
            <div class="modal-footer">
                <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="add()">保存</button>
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
            </div>
        </div>
    </div>
</div>

<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="myModalLabel">商品分类编辑</h3>
            </div>
            <div class="modal-body">
                <form id="updateForm">
                    <table class="table table-bordered table-striped" width="800px" id="updateTable">
                    </table>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="update()">保存</button>
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    //token
    var token = window.localStorage.getItem("token")

    var parentId = 0

    $(function () {
        getItemCat(parentId)
    })

    //查询全部
    function getItemCat(pid, name) {
        $("#aaa").val(pid)
        $.ajax({
            url: domain + "/cat/catList?parentId=" + pid,
            contentType: 'application/json;charset=utf-8',
            headers: {"AutharizeToken":token,"Content-Type":"application/json;charset=UTF-8"},
            type: "post",
            dataType: "json",
            async: false,
            success: function (result) {
                var data = result.data;
                var html = '';
                $(data).each(function (i, e) {
                    html += '<tr>'
                    html += '<td><input type="checkbox" value="' + e.id + '"></td>'
                    html += '<td>' + e.id + '</td>'
                    html += '<td>' + e.name + '</td>'
                    html += '<td>' + e.typeId + '</td>'
                    html += '<td class="text-center">'
                    html += '<input type="button" class="btn bg-olive btn-xs" onclick="getItemCat(' + e.id + ',\'' + e.name + '\')" value="查询下级">'
                    html += '<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" onclick="toUpdate(' + e.id + ')">修改'
                    html += '</button>'
                    html += '</td>'
                    html += '</tr>'
                })
                $("#cat").html(html)
                //更新parentId
                parentId = pid
                //分类
                var fenlei = '<li><a href = "#" onClick = "getItemCat(0)">顶级分类列表</a></li>'
                if (name != undefined) {
                    fenlei += '<li><a href = "#" onClick = "getItemCat(' + pid + ',\'' + name + '\')">' + name + '</a></li>'
                }

                $("#item").html(fenlei)

            },
            error: function (result) {
                alert("系统出错!")
            }
        })
    }

    //增加
    function add() {
        var itName = $("#itName").val();
        var tId = $('#itypeId').val();
        var parentId = $('#aaa').val();
        var cat = {
            "parentId": parentId,
            "name": itName,
            "typeId": tId
        }
        $.ajax({
            url: domain + "/cat/add",
            type: "post",
            dataType: "json",
            contentType: "application/json; charset=utf-8",//这个是告诉后台我传的是json过去的
            //JSON.stringify() 将字符串转成json对象
            data: JSON.stringify(cat),
            async: false,
            success: function (result) {
                alert(result.message);
                window.location.reload();
            },
            error: function (result) {
                alert(result.message);
            }
        });
    }

    //回显
    function toUpdate(id) {
        $.ajax({
            url: domain + "/cat/selectById?id=" + id,
            //contentType: 'application/json;charset=utf-8',
            headers: {"AutharizeToken":token,"Content-Type":"application/json;charset=UTF-8"},
            type: "post",
            //dataType: "json",
            async: false,
            success: function (result) {
                if (result.code == 0) {
                    var data = result.data;
                    var html = '<tr>'
                        + '<input class="form-control" value="' + data.id + '" type="hidden" id="id" name="id">'
                        + '<input class="form-control" value="' + data.parentId + '" type="hidden" id="parentId" name="parentId">'
                        + '<td>商品分类名称</td>'
                        + '<td><input class="form-control" value="' + data.name + '" id="name" name="name"></td>'
                        + '</tr>'
                        + '<tr>'
                        + '<td>类型模板</td>'
                        + '<td><input select2 class="form-control" id="typeId" name="typeId" type="text" value="' + data.typeId + '"/></td>'
                        + '</tr>'
                    $("#updateTable").html(html)
                }
            },
            error: function (result) {
                alert("系统出错!")
            }
        })
    }

    //更新
    function update() {
        var cat = {
            name: $("#name").val(),
            typeId: $("#typeId").val(),
            id: $("#id").val(),
            parentId: $("#parentId").val()
        }
        console.log(cat)
        $.ajax({
            url: domain + "/cat/update",
            contentType: 'application/json;charset=utf-8',
            headers: {"AutharizeToken":token,"Content-Type":"application/json;charset=UTF-8"},
            type: "put",
            dataType: "json",
            async: false,
            data: JSON.stringify(cat),
            success: function (result) {
                if (result.code == 0) {
                    alert(result.message)
                    window.location.reload();
                }
            },
            error: function (result) {
                alert("系统出错!")
            }
        })
    }

    //删除
    function deleteItemCat() {
        var ids = []  //定义一个数组
        $("[type=checkbox]").each(function (i, e) {   //获取所有复选框的value,循环
            if ($(e).prop("checked")) {   //判断是否被选中
                ids.push($(e).val())  //选中就添加到数组中
            }
        })
        $.ajax({
            url: domain + "/cat/delete",
            contentType: 'application/json;charset=utf-8',
            headers: {"AutharizeToken":token,"Content-Type":"application/json;charset=UTF-8"},
            type: "delete",
            dataType: "json",
            data: JSON.stringify(ids),
            async: false,
            success: function (result) {
                if (result.code == 0) {
                    alert("删除成功")
                    window.location.reload();
                }
            },
            error: function (result) {
                alert("系统出错!")
            }
        })
    }

</script>
</html>